package com.hdoit.ares.logical.acc.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.acc.dao.IAccountInfoDao;
import com.hdoit.ares.logical.acc.model.AccountDetail;
import com.hdoit.ares.logical.acc.model.AccountInfo;
import com.hdoit.ares.logical.acc.model.GoldenBeanDetail;
import com.hdoit.ares.logical.acc.model.IncomeDetail;
import com.hdoit.ares.logical.acc.model.IncomeInfo;
import com.hdoit.ares.logical.acc.service.IAccountDetailService;
import com.hdoit.ares.logical.acc.service.IAccountInfoService;
import com.hdoit.ares.logical.acc.service.IGoldenBeanDetailService;
import com.hdoit.ares.logical.acc.service.IIncomeDetailService;
import com.hdoit.ares.logical.acc.service.IIncomeInfoService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.acc.AccountInfoSearch;
import com.hdoit.ares.selfcommon.enums.acc.GoldenBeanType;
import com.hdoit.ares.selfcommon.enums.acc.HesitatePeriodState;
import com.hdoit.ares.selfcommon.enums.acc.IncomeState;
import com.hdoit.ares.selfcommon.enums.acc.MoneyType;
import com.hdoit.ares.selfcommon.enums.acc.State;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.logical.setting.dao.IParamOptionDao;
import com.tennetcn.common.logical.setting.model.ParamOption;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.common.utils.RandomUtils;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2017年11月27日 09:03:56
 * @comment
 */
@Component
public class AccountInfoServiceImpl extends SuperService<AccountInfo> implements IAccountInfoService{
	
	public final static String StrAccountNo = "yyyyMMddHHmmss";
	
	private IAccountInfoDao accountInfoDao;
	   
	public IAccountInfoDao getAccountInfoDao() {
		return accountInfoDao;
	}
    
	@Resource
	public void setAccountInfoDao(IAccountInfoDao accountInfoDao) {
		this.accountInfoDao = accountInfoDao;
	}
	
	private IAccountInfoService accountInfoService;
	private IAccountDetailService accountDetailService;
	private IGoldenBeanDetailService beanDetailService;
	private IParamOptionDao paramOptionDao;
	private IIncomeInfoService iIncomeInfoService;
	private IIncomeDetailService iIncomeDetailService;
	
	public IAccountInfoService getAccountInfoService() {
		return accountInfoService;
	}
	@Resource
	public void setAccountInfoService(IAccountInfoService accountInfoService) {
		this.accountInfoService = accountInfoService;
	}

	public IAccountDetailService getAccountDetailService() {
		return accountDetailService;
	}
	@Resource
	public void setAccountDetailService(IAccountDetailService accountDetailService) {
		this.accountDetailService = accountDetailService;
	}

	public IParamOptionDao getParamOptionDao() {
		return paramOptionDao;
	}
	@Resource
	public void setParamOptionDao(IParamOptionDao paramOptionDao) {
		this.paramOptionDao = paramOptionDao;
	}

	public IGoldenBeanDetailService getBeanDetailService() {
		return beanDetailService;
	}
	@Resource
	public void setBeanDetailService(IGoldenBeanDetailService beanDetailService) {
		this.beanDetailService = beanDetailService;
	}

	public IIncomeInfoService getiIncomeInfoService() {
		return iIncomeInfoService;
	}
	@Resource
	public void setiIncomeInfoService(IIncomeInfoService iIncomeInfoService) {
		this.iIncomeInfoService = iIncomeInfoService;
	}

	public IIncomeDetailService getiIncomeDetailService() {
		return iIncomeDetailService;
	}
	@Resource
	public void setiIncomeDetailService(IIncomeDetailService iIncomeDetailService) {
		this.iIncomeDetailService = iIncomeDetailService;
	}

	@Override
	public AccountInfo queryModelByCustomerId(String id){
		return accountInfoDao.queryModelByCustomerId(id);
	}
	
	@Override
	public boolean addAccountInfo(Customer customer){
		AccountInfo accountInfo = new AccountInfo();
		accountInfo.setId(CommonUtils.getKeyId());
		accountInfo.setAccountNo(DateUtils.convertDate2String(StrAccountNo, DateUtils.getCurrent())+RandomUtils.randomInt(1000, 9999));
		accountInfo.setCustomerId(customer.getId());
		accountInfo.setAccount(String.valueOf(customer.getUserIdentity()));
		accountInfo.setName(customer.getNickname());
		accountInfo.setBalance(0);
		accountInfo.setGoldenBean(0);
		accountInfo.setModelStatus(ModelStatus.add);		
		return applyChange(accountInfo);
	}

	@Override
	public List<AccountInfo> queListByCustomerId(String id) {
		return accountInfoDao.querListByCustomerId(id);
	}

	@Override
	public List<AccountInfo> queryList(AccountInfoSearch accountInfoSearch,PagerModel pagerModel) {
		return accountInfoDao.queryList(accountInfoSearch,pagerModel);
	}

	@Override
	public Boolean updateGoldToBalance(Customer customer, Double money) {

		ParamOption paramOption = paramOptionDao.queryModelByParamName("exchange");
		int exchangeRate = Integer.valueOf(paramOption.getParamValue());
		
		//第一步  账户表   金豆减少    余额增多
		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		accountInfo.setGoldenBean(accountInfo.getGoldenBean() - money);
		accountInfo.setBalance(accountInfo.getBalance() + money/exchangeRate);
		
		accountInfo.setModelStatus(ModelStatus.update);
		
		//第二步  账户明细表  添加记录
		AccountDetail accountDetail = new AccountDetail();
		accountDetail.setId(CommonUtils.getKeyId());
		accountDetail.setAccount(customer.getUserIdentity()+"");
		accountDetail.setCustomerId(customer.getId());
		accountDetail.setAccountNo(accountInfo.getAccountNo());
		accountDetail.setMoneyType(MoneyType.goldTransferToBalance);
		accountDetail.setServiceId("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));

		accountDetail.setState(State.income);
		//业务id set进入
		accountDetail.setTime(DateUtils.getCurrent());
		accountDetail.setMoney(money/exchangeRate);
		accountDetail.setBalance(accountInfo.getBalance());
		
		accountDetail.setModelStatus(ModelStatus.add);
		
		//第二步  金豆明细表  添加记录
		GoldenBeanDetail goldenBeanDetail = new GoldenBeanDetail();
	    goldenBeanDetail.setId(CommonUtils.getKeyId());
	    goldenBeanDetail.setAccountNo(accountInfo.getAccountNo());
	    goldenBeanDetail.setGoldenBeanType(GoldenBeanType.TransferToBalance);
	    goldenBeanDetail.setTime(DateUtils.getCurrent());
	    goldenBeanDetail.setState(State.pay);
	    goldenBeanDetail.setGoldenBeanAccount(money);
	    goldenBeanDetail.setGoldenBeanBalance(accountInfo.getGoldenBean());
	    goldenBeanDetail.setCustomerId(customer.getId());
	    goldenBeanDetail.setComment("金豆转入余额");
	    goldenBeanDetail.setHesitatePeriodState(HesitatePeriodState.Hesitated);
	    goldenBeanDetail.setOutTradeNo("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
	    
	    goldenBeanDetail.setModelStatus(ModelStatus.add);
		
	    return accountInfoService.applyChange(accountInfo) && accountDetailService.applyChange(accountDetail) && beanDetailService.applyChange(goldenBeanDetail);
	}

	@Override
	public Boolean updateIncomeToBalance(Customer customer, Double money) {
		
		//第一步  账户表    余额增多
		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		accountInfo.setBalance(accountInfo.getBalance() + money);
		
		accountInfo.setModelStatus(ModelStatus.update);
		
		//第二步 奖金表 奖金减少
		IncomeInfo incomeInfo = iIncomeInfoService.queryIncomeInfo(customer.getId());
		incomeInfo.setIncomeBalance(incomeInfo.getIncomeBalance() - money);
		
		incomeInfo.setModelStatus(ModelStatus.update);
		
		//第三步  账户明细表  添加记录
		AccountDetail accountDetail = new AccountDetail();
		accountDetail.setId(CommonUtils.getKeyId());
		accountDetail.setAccount(customer.getUserIdentity()+"");
		accountDetail.setCustomerId(customer.getId());
		accountDetail.setAccountNo(accountInfo.getAccountNo());
		accountDetail.setMoneyType(MoneyType.incomeTransferToBalance);
		accountDetail.setServiceId("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
		
		accountDetail.setState(State.income);
		//业务id set进入
		accountDetail.setTime(DateUtils.getCurrent());
		accountDetail.setMoney(money);
		accountDetail.setBalance(accountInfo.getBalance());
		
		accountDetail.setModelStatus(ModelStatus.add);
		
		//第四步  奖金明细表  添加记录
		IncomeDetail incomeDetail = new IncomeDetail();
		incomeDetail.setId(CommonUtils.getKeyId());
		incomeDetail.setAccountNo(accountInfo.getAccountNo());
		incomeDetail.setIncomeState(IncomeState.TransferToBalance);
		incomeDetail.setTime(DateUtils.getCurrent());
		incomeDetail.setState(State.pay);
		incomeDetail.setIncomeAccount(money);
		incomeDetail.setIncomeBalance(incomeInfo.getIncomeBalance());
		incomeDetail.setCustomerId(customer.getId());
		incomeDetail.setComment("奖金转入余额");	    
		incomeDetail.setHesitatePeriodState(HesitatePeriodState.Hesitated);
		incomeDetail.setOutTradeNo("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
		
		incomeDetail.setModelStatus(ModelStatus.add);
		
	    return accountInfoService.applyChange(accountInfo) && accountDetailService.applyChange(accountDetail) && iIncomeInfoService.applyChange(incomeInfo) && iIncomeDetailService.applyChange(incomeDetail);
	}

	@Override
	public Boolean toBalance(Customer customer, Double money) {
		
		//第一步  账户表    余额增多
		AccountInfo accountInfo = accountInfoService.queryModelByCustomerId(customer.getId());
		accountInfo.setBalance(accountInfo.getBalance() + money);
		
		accountInfo.setModelStatus(ModelStatus.update);
		
		//第二步 活动表奖金标识
		
		//第三步  账户明细表  添加记录
		AccountDetail accountDetail = new AccountDetail();
		accountDetail.setId(CommonUtils.getKeyId());
		accountDetail.setAccount(customer.getUserIdentity()+"");
		accountDetail.setCustomerId(customer.getId());
		accountDetail.setAccountNo(accountInfo.getAccountNo());
		accountDetail.setMoneyType(MoneyType.bonus);
		accountDetail.setServiceId("" + System.currentTimeMillis()+RandomUtils.randomInt(1000, 9999));
		
		accountDetail.setState(State.income);
		//业务id set进入
		accountDetail.setTime(DateUtils.getCurrent());
		accountDetail.setMoney(money);
		accountDetail.setBalance(accountInfo.getBalance());
		
		accountDetail.setModelStatus(ModelStatus.add);
		
	    return accountInfoService.applyChange(accountInfo) && accountDetailService.applyChange(accountDetail);
	}
	
	@Override
	public Double queryGoldBean(String id) {
		
		return accountInfoDao.queryGoldBean(id);
	}

	@Override
	public Double queryAllBalance() {
		return accountInfoDao.queryAllBalance();
	}
	

	
	
}